package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.WebsiteAddrDao;
import com.hxkj.domain.po.WebsiteAddr;
import com.hxkj.service.WebsiteAddrService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class WebsiteAddrServiceImpl implements WebsiteAddrService{

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WebsiteAddrDao websiteAddrDao;
    @Override
    public PageInfo<WebsiteAddr> getPages(WebsiteAddr website, PageTo pageParam) throws Exception {
        try {
            Page<WebsiteAddr> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            website.setDelTag(0L);
            List<WebsiteAddr> websiteAddrs = websiteAddrDao.get(website);
            PageInfo<WebsiteAddr> pageInfo = new PageInfo<>(page);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<WebsiteAddr> getList(WebsiteAddr website) throws Exception {
        try {
            website.setDelTag(0L);
            return websiteAddrDao.get(website);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultDataStruct<WebsiteAddr> get(WebsiteAddr websiteAddr) throws Exception {
        ResultDataStruct result = new ResultDataStruct();
        try {
            if (redisTemplate.hasKey("WEBSITE_ADD" + websiteAddr.getSiteId())) {
                WebsiteAddr websiteAddr1 = (WebsiteAddr) redisTemplate.boundValueOps("WEBSITE_ADD" + websiteAddr.getSiteId()).get();
                result.setCode(200);
                result.setData(websiteAddr1);
                return result;
            }
            WebsiteAddr websiteAddr2;
            if(websiteAddr.getAddrId() == null){
                List<WebsiteAddr> websiteAddrList = websiteAddrDao.getWebsiteAddrBySiteId(websiteAddr);
                websiteAddr2 = websiteAddrList.get(0);
            }else {
                websiteAddr2 = websiteAddrDao.get(websiteAddr.getAddrId());
            }
            result.setCode(200);
            result.setData(websiteAddr2);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultMessageStruct modifyWebsite(WebsiteAddr websiteAddr) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            redisTemplate.delete("WEBSITE_ADD" + websiteAddr.getSiteId());

            websiteAddr.setUpdateTime(System.currentTimeMillis());

            websiteAddrDao.update(websiteAddr);
            result.setCode(200);
            //更新Redis中的缓存
            WebsiteAddr websiteAddr_ = websiteAddrDao.get(websiteAddr.getAddrId());

            log.error("当前的站点{},{}",websiteAddr_.getUrlPrefix(),websiteAddr_.getSourcesDirectory());

            redisTemplate.opsForValue().set("WEBSITE_ADD" + websiteAddr_.getSiteId(),websiteAddr_);
            //redisTemplate.boundValueOps("WEBSITE_ADD" + websiteAddr_.getSiteId()).set(websiteAddr_);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public ResultMessageStruct saveWebsite(WebsiteAddr websiteAddr) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            //一个站点只能有一条数据
            WebsiteAddr websiteAddr1 = new WebsiteAddr();
            websiteAddr1.setSiteId(websiteAddr.getSiteId());
            List<WebsiteAddr> websiteAddrs = websiteAddrDao.get(websiteAddr1);
            if (websiteAddrs.size() > 0) {
                result.setCode(201);
                result.setMessage("本站已存在url配置规则数据,请勿重复添加!");
                return result;
            }
            websiteAddr.setCreateTime(System.currentTimeMillis());
            websiteAddr.setUpdateTime(System.currentTimeMillis());
            int i = websiteAddrDao.save(websiteAddr);
            if (i == 1) {
                result.setCode(200);
                //更新Redis中的缓存
                WebsiteAddr websiteAddr_ = websiteAddrDao.get(websiteAddr.getAddrId());
                redisTemplate.boundValueOps("WEBSITE_ADD" + websiteAddr_.getSiteId()).set(websiteAddr_);
            } else {
                result.setCode(401);
            }
            result.setMessage(i == 1 ? "添加成功!" : "添加失败!");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultMessageStruct removeWebsite(WebsiteAddr websiteAddr) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            String[] addrIds = websiteAddr.getAddrIds().split(",");
            for (String addrId : addrIds) {
                //更新Redis中的缓存
                WebsiteAddr websiteAddr_ = websiteAddrDao.get(Long.valueOf(addrId));
                redisTemplate.delete("WEBSITE_ADD" + websiteAddr_.getSiteId());
                websiteAddrDao.delete(Long.valueOf(addrId));
            }
            result.setCode(200);
            return result;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
